/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
 var intersect = function(nums1, nums2) {
  let hash = {}
  for(let i of nums1){
    if(!hash[i]){
      hash[i] = 1
    }else{
      hash[i]++
    }
  }
  let hash2 = {}
  for(let i of nums2){
    if(!hash2[i]){
      hash2[i] = 1
    }else{
      hash2[i]++
    }
  }
  let res = []
  for(let i in hash){
    // console.log(i);
    if(hash[i] && hash2[i]){
      let len = hash[i] < hash2[i] ? hash[i] : hash2[i]
      for(let j = 0;j < len;j++){
        res.push(i)
      }
    }
  }
  return res
};
console.log(intersect([4,9,5],[9,4,9,8,4]));